package com.hsteach.web.controller.wage;

import com.google.common.collect.Maps;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.domain.R;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.framework.feign.client.upload.vo.UploadRet;
import com.hsteach.framework.web.service.UploadService;
import com.hsteach.util.WageTemplateUtil;
import com.hsteach.dao.domain.wage.WageTemplate;
import com.hsteach.framework.config.ServerConfig;
import com.hsteach.service.wage.WageTemplateItemsService;
import com.hsteach.service.wage.WageTemplateService;
import com.hsteach.web.controller.wage.dto.AddWageTemplateDto;
import com.hsteach.web.controller.wage.dto.UpdateWageTemplateDto;
import com.hsteach.web.controller.wage.dto.WageTemplateItemDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工资模版Controller
 * 
 * @author fanxquer
 * @date 2023-07-13
 */
@Api(value = "工资模版", tags = "工资模版")
@RestController
@RequestMapping("/web/wage/template")
public class WageTemplateController extends BaseController
{
    @Autowired
    private WageTemplateService wageTemplateService;

    @Autowired
    private WageTemplateItemsService wageTemplateItemsService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private UploadService uploadService;

    /**
     * 查询工资模版列表
     */
    @ApiOperation("查询工资模版列表")
    @PostMapping("/list")
    public TableDataInfo list()
    {
//        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<WageTemplate> list = wageTemplateService.selectWageTemplateList(new WageTemplate());
        return getDataTable(list);
    }

    /**
     * 导出工资模版
     */
    @PostMapping("/export/{id}")
    @ApiOperation("导出工资模版")
    public AjaxResult export(HttpServletResponse response, @PathVariable("id") Long id)
    {
        UpdateWageTemplateDto template = wageTemplateService.selectWageTemplateById(id);
        WageTemplateUtil<WageTemplate> util = new WageTemplateUtil<WageTemplate>(WageTemplate.class);
        Workbook wb = util.exportTitleWb(template.getItems().stream().filter(e -> BooleanEnum.TRUE.getCode().equals(e.getStatus())).map(WageTemplateItemDto::getItemName).collect(Collectors.toList()), template.getTemplateName() + ".xlsx");
        UploadRet url = uploadService.upload(wb, template.getTemplateName() + ".xlsx");
        AjaxResult success = AjaxResult.success();
        Map<String, String> data = Maps.newHashMap();
        data.put("url", url.getUrl());
        success.put("data", data);
        return success;
    }

    /**
     * 获取工资模版详细信息
     */
    @ApiOperation("获取工资模版详细信息")
    @GetMapping(value = "/{id}")
    public R<UpdateWageTemplateDto> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(wageTemplateService.selectWageTemplateById(id));
    }

    /**
     * 新增工资模版
     */
    @ApiOperation("新增工资模版")
    @PostMapping
    public AjaxResult add(@RequestBody AddWageTemplateDto dto)
    {
        return toAjax(wageTemplateService.addWageTemplate(dto));
    }

    /**
     * 复制工资模版
     */
    @ApiOperation("复制工资模版")
    @PostMapping("/copy/{id}")
    public AjaxResult copy(@PathVariable Long id)
    {
        return toAjax(wageTemplateService.copy(id));
    }

    /**
     * 修改工资模版
     */
    @ApiOperation("修改工资模版")
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateWageTemplateDto dto)
    {
        return toAjax(wageTemplateService.updateWageTemplate(dto));
    }

    /**
     * 设置默认工资模版
     */
    @ApiOperation("设置默认工资模版")
    @PutMapping("/default/{id}")
    public AjaxResult updateDefault(@PathVariable Long id)
    {
        return toAjax(wageTemplateService.updateDefault(id));
    }

    /**
     * 删除工资模版
     */
    @ApiOperation("删除工资模版")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(wageTemplateService.deleteWageTemplateById(id));
    }

    /**
     * 删除工资模版数据项
     */
    @ApiOperation("删除工资模版数据项")
    @DeleteMapping("/item/{id}")
    public AjaxResult removeItem(@PathVariable Long id)
    {
        return toAjax(wageTemplateItemsService.deleteWageTemplateItemsById(id));
    }
}
